/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.fachadas;

import edu.unicauca.opentextbook.entidades.Tarea;
import edu.unicauca.opentextbook.entidades.VotosTarea;
import edu.unicauca.opentextbook.entidades.VotosTarea;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
import javax.persistence.Query;
import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Gamificaion
 */
@Stateless
public class VotosTareaFacade extends AbstractFacade<VotosTarea> {

    @PersistenceContext(unitName = "openTextBookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public VotosTareaFacade() {
        super(VotosTarea.class);
    }

    public List<Tarea> cargarVotosPorCubrir(int idLibro) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Tarea> listaTareas = null;

        sql.append("SELECT * FROM tarea WHERE idTarea IN ");
        sql.append("(SELECT idTarea FROM ");
        sql.append("(SELECT vt.idTarea, COUNT(*) AS votosPositivos, ");
        sql.append("(SELECT COUNT(*) FROM votos_tarea v ");
        sql.append("INNER JOIN tarea t ON v.idTarea  = t.idTarea AND t.idlibro = ? ");
        sql.append("WHERE v.valor = 0 AND v.idTarea = vt.idTarea ) AS votosNegativos ");
        sql.append("FROM votos_tarea vt ");
        sql.append("INNER JOIN tarea ta ON vt.idTarea = ta.idTarea AND ta.idlibro = ? ");
        sql.append("WHERE vt.valor = 1 ");
        sql.append("GROUP BY vt.idTarea ");
        sql.append("HAVING COUNT(*) >= (SELECT COUNT(*) ");
        sql.append("FROM votos_tarea v ");
        sql.append("INNER JOIN tarea t ON v.idTarea = t.idTarea AND t.idlibro = ? ");
        sql.append("WHERE v.valor = 0 ");
        sql.append("AND v.idTarea = vt.idTarea )");
        sql.append("ORDER BY vt.idTarea))");

        try {
            query = em.createNativeQuery(sql.toString(), Tarea.class);
            query.setParameter(1, idLibro);
            query.setParameter(2, idLibro);
            query.setParameter(3, idLibro);

            listaTareas = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_NoResultException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_IllegalStateException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_QueryTimeoutException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_TransactionRequiredException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_PessimisticLockException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_LockTimeoutException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_PersistenceException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorCubrir_Exception:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } finally {
            return listaTareas;
        }
    }

    public List<VotosTarea> cargarVotosPorUsuarioYlibro(int idUsuario, int idLibro) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<VotosTarea> listaTareas = null;

        sql.append("SELECT vt.* ");
        sql.append("FROM votos_tarea vt ");
        sql.append("INNER JOIN tarea t ON vt.idTarea = t.idTarea AND vt.idUsuario = ? ");
        sql.append("INNER JOIN libro l ON t.idLibro = l.idLibro AND l.idLibro = ? ");
        sql.append("ORDER BY vt.idVotos_tarea");

        try {
            query = em.createNativeQuery(sql.toString(), VotosTarea.class);
            query.setParameter(1, idUsuario);
            query.setParameter(2, idLibro);

            listaTareas = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_NoResultException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_IllegalStateException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_QueryTimeoutException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_TransactionRequiredException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_PessimisticLockException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_LockTimeoutException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_PersistenceException:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_cargarVotosPorUsuarioYlibro_Exception:".concat(e.getMessage()));
            listaTareas = new ArrayList<VotosTarea>();
        } finally {
            return listaTareas;
        }
    }
}
